package com.whfp.coky.ui.main.vm

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.whfp.coky.databinding.ActivityDruggerTaskCountBinding
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.BaseViewModel
import com.whfp.coky.ui.base.model.RequestBean
import com.whfp.coky.ui.main.model.TaskCount01Bean

/**
 *  初始化manid时，初始化本地的请求body，完成数据请求加载
 *  下拉刷新的时候再进行请求
 *
 * created by coky on 2022/3/14
 */
class DuggerTaskCountVM:BaseViewModel<ActivityDruggerTaskCountBinding>() {

    /**
     * {"result":1,"msg":"success","data":
     * {"todo":[{"number":0,"task":[],"name":"待办任务"},{"number":"0","name":"完成任务"},{"number":"0","name":"过期任务"}]}}
      */
    var countLD = MutableLiveData<TaskCount01Bean>()//http返回的数据
    var manIdLD = MutableLiveData<String>()//act跳转传入的类
    lateinit var reqCount : ReqTaskCount//请求bean


    fun httpCount(showLoading:Boolean){
        manIdLD.value?.let {
            launch({httpUtil.doDruggerTaskCount(reqCount)},countLD,showLoading,true)
        }

    }

    override fun observe(activity: BaseActivity<*, *>, owner: LifecycleOwner) {
        super.observe(activity, owner)
        countLD.observe(activity, Observer {
            vBinding.refreshLayoutTaskCount.finishRefresh()
            vBinding.tvWc.text = it.completedNum.toString()
            vBinding.tvDb.text = it.executedNum.toString()
            vBinding.tvGq.text = it.expiredNum.toString()
        })
        manIdLD.observe(activity, Observer {
            reqCount = ReqTaskCount(it)
            httpCount(true)
        })
    }


    /**
     * 统计接口请求bean
     */
    class ReqTaskCount(var manId: String): RequestBean(){
        val equipment:String = "115"
        val system:String = "118"
        override fun toString(): String {
            return "ReqTasks(manId='$manId', equipment='$equipment', system='$system')"
        }
    }

}